package com.demo.androidgame.samgukji_hit;

import java.util.List;

import javax.microedition.khronos.opengles.GL10;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;

import com.demo.androidgame.fx.Game;
import com.demo.androidgame.fx.Input.KeyEvent;
import com.demo.androidgame.fx.Input.TouchEvent;
import com.demo.androidgame.fx.gl.Camera2D;
import com.demo.androidgame.fx.gl.SpriteBatcher;
import com.demo.androidgame.fx.impl.GLScreen;
import com.demo.androidgame.fx.math.OverlapTester;
import com.demo.androidgame.fx.math.Rectangle;
import com.demo.androidgame.fx.math.Vector2;

public class HeroStageScreen extends GLScreen {

	static final int GAME_READY = 0;    
    static final int GAME_RUNNING = 1;
    static final int GAME_PAUSED = 2;
    static final int GAME_LEVEL_END = 3;
    static final int GAME_OVER = 4;
	
	
	Camera2D guiCam;
	Vector2 touchPoint;
	SpriteBatcher batcher;
	HeroStage herostage;
	HeroStageRenderer heroStageRenderer;
	S_Arrow arrow;
	
	Rectangle leftFire;
	Rectangle rightFire;
	float X2, Y2, X3, Y3;

	public HeroStageScreen(Game game) {
		super(game);
		guiCam = new Camera2D(glGraphics, 800, 480);
		batcher = new SpriteBatcher(glGraphics, 1000);

		touchPoint = new Vector2();

		herostage = new HeroStage();
		heroStageRenderer = new HeroStageRenderer(glGraphics, batcher,
				herostage);
		X2 = herostage.cam.position.x + 140 + 62.5f;
		Y2 = herostage.cam.position.y - 180;
		X3 = herostage.cam.position.x + 140 + 62.5f + 125;
		Y3 = herostage.cam.position.y - 180;
		
		leftFire = new Rectangle(X2 - 62.5f, Y2 - 50, 125, 100);
		rightFire = new Rectangle(X3 - 62.5f, Y3 - 50, 125, 100);
		
	}
	
	
//	public boolean onKeyDown(int keyCode, KeyEvent event) {
//		 if( keyCode == KeyEvent.KEYCODE_BACK ) {
//			 AlertDialog.Bu
//		 }
//		  
//	}
//		   AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_alert).setTitle("Quit").setMessage("Do you want to quit").setPositiveButton("Yes", new DialogInterface.OnClickListener()
//		   {
//		     @Override
//		     public void onClick( DialogInterface dialog, int which )
//		     {
//		    	 game.setScreen(new GameScreen(game));
//		     }
//		   }).setNegativeButton( "No", null ).show(); 
//		  
//		   return true;
//		  }
//		  
//		  return super.onKeyDown(keyCode, event);
//
//	}

	
	@Override
	public void update(float deltaTime) {
		List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
		List<KeyEvent> keyEvents = game.getInput().getKeyEvents();
		
		int key = keyEvents.size();
		for (int i = 0; i < key; i++) {
			KeyEvent event2 = keyEvents.get(i);
			if(event2.type == KeyEvent.KEY_DOWN) {
				if (event2.keyCode == android.view.KeyEvent.KEYCODE_BACK) {
					((Activity)game).runOnUiThread(new Runnable() {
						
						@Override
						public void run() {
							new AlertDialog.Builder((Activity)game).setIcon(android.R.drawable.ic_dialog_alert).setTitle("Quit").setMessage("Do you want to quit").setPositiveButton("Yes", new DialogInterface.OnClickListener() {
								
								@Override
								public void onClick(DialogInterface dialog, int which) {
									game.setScreen(new ActOneScreen(game));
									
								}
							}).setNegativeButton( "No", null ).show(); 
						}
					});
					
					
				}
			}
		}

		int len = touchEvents.size();
		for (int i = 0; i < len; i++) {
			TouchEvent event = touchEvents.get(i);
			herostage.updateJoystick(event);

	            if(event.type == TouchEvent.TOUCH_UP) {
	                touchPoint.set(event.x, event.y);
	                guiCam.touchToWorld(touchPoint);
	                
	                if (OverlapTester.pointInRectangle(leftFire, touchPoint)) {
//	                	herostage.arrow.leftFire();
//	                	arrow.state = S_Arrow.STATE_ARROW_FIRE_LEFT;
	                	herostage.update(deltaTime);
	                	herostage.addArrow(herostage.hero.position.x, herostage.hero.position.y);
	                	heroStageRenderer.renderArrow();
	                	herostage.hero.heroFire();
//	                	arrow.position.x -= 4;
	                	return;
	                }
	                if (OverlapTester.pointInRectangle(rightFire, touchPoint)) {
//	                	herostage.arrow.rightFire();
//	                	arrow.state = S_Arrow.STATE_ARROW_FIRE_RIGHT;
	                	herostage.update(deltaTime);
	                	herostage.addArrow(herostage.hero.position.x, herostage.hero.position.y);
	                	heroStageRenderer.renderArrow();
	                	herostage.hero.heroFire();
//	                	arrow.position.x += 4;
	                	return;
	                }
	            }
	        }			
		herostage.update(deltaTime);

	}

	@Override
	public void present(float deltaTime) {
		GL10 gl = glGraphics.getGL();
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		gl.glEnable(GL10.GL_TEXTURE_2D);
		
		heroStageRenderer.render();
		
//		batcher.beginBatch(Assets.keypad);
//		batcher.drawSprite(_controls._pointerPosition.x, _controls._pointerPosition.y, 100, 100, Assets.keypadRegion);
//		batcher.endBatch();
		
		guiCam.setViewportAndMatrices();
		gl.glDisable(GL10.GL_TEXTURE_2D);

	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub

	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub

	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}
	
	
}
